//
//  BlockActionSheet.m
//  BlockAlertView
//
//  Created by cocoajin on 14-1-13.
//  Copyright (c) 2014年 www.zhgu.net. All rights reserved.
//

#import "BlockActionSheet.h"

@interface BlockActionSheet ()<UIActionSheetDelegate>

@property (nonatomic,copy)KKActionSheetBlock kkClickBlock;

@end

@implementation BlockActionSheet

@synthesize kkClickBlock;

+ (instancetype)shareActionSheet
{
    static BlockActionSheet *shareActionSheet;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareActionSheet = [[BlockActionSheet alloc]init];
        
    });
    
    return shareActionSheet;
}

+ (void)showActionSheet:(NSString *)title cancel:(NSString *)cancel destruct:(NSString *)destruct otherBtns:(NSArray *)arrayBtns sheetStyle:(UIActionSheetStyle )sheetStyle clickBlock:(KKActionSheetBlock )clickBlock
{
    [[BlockActionSheet shareActionSheet] showActionSheet:title cancel:cancel destruct:destruct otherBtns:arrayBtns sheetStyle:sheetStyle clickBlock:clickBlock];
}

- (void)showActionSheet:(NSString *)title cancel:(NSString *)cancel destruct:(NSString *)destruct otherBtns:(NSArray *)arrayBtns sheetStyle:(UIActionSheetStyle )sheetStyle clickBlock:(KKActionSheetBlock )clickBlock
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:title delegate:self cancelButtonTitle:cancel destructiveButtonTitle:destruct otherButtonTitles:nil, nil];
    actionSheet.actionSheetStyle = sheetStyle;
    self.kkClickBlock = clickBlock;
    Block_release(clickBlock);
    clickBlock = nil;
    
    for (NSString *btnTitle in arrayBtns) {
        [actionSheet addButtonWithTitle:btnTitle];
    }
    
    [actionSheet showInView:[[UIApplication sharedApplication].windows objectAtIndex:0]];
    
    [actionSheet autorelease];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (kkClickBlock) {
        kkClickBlock(actionSheet,buttonIndex);
    }
}

- (void)dealloc
{
    [[BlockActionSheet shareActionSheet] release];
    kkClickBlock = nil;
    [super dealloc];
}

@end
